import 'package:fate_blind_box/network/http_request.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/utils/default/common_config.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../main/model/shop_matchmaker_list_model.dart';
import 'package:get/get.dart';
import '../map_navgation_utils.dart';

class MatchShopDetailPage extends StatefulWidget {
  ShopMatchMakerListDataModel? detailModel;
  String? disStr;

  MatchShopDetailPage({Key? key, this.detailModel, this.disStr})
      : super(key: key);

  @override
  State<MatchShopDetailPage> createState() => _MatchShopDetailPageState();
}

class _MatchShopDetailPageState extends State<MatchShopDetailPage> {
  double itemHeight = 60;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Image.network(
          widget.detailModel?.matchPhoto ?? '',
          width: 1.sw,
        ),
        Positioned(
          top: 208,
          left: 0,
          right: 0,
          bottom: 0,
          child: Container(
            padding: const EdgeInsets.only(left: 20, top: 20, right: 17),
            decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20),
                    topRight: Radius.circular(20))),
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    widget.detailModel?.shortName ?? '',
                    style:
                        TextStyle(fontSize: 18, color: ThemeColor.textColor3),
                  ),
                  const SizedBox(
                    height: 20,
                  ),
                  Container(
                    height: 1,
                    color: ThemeColor.dividerColor,
                  ),
                  const SizedBox(
                    height: 12,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          RichText(
                            text: TextSpan(
                                children: [
                                  TextSpan(
                                      text:
                                          '${widget.detailModel?.openTime ?? '9:00'}-${widget.detailModel?.endTime ?? '22:00'}',
                                      style: const TextStyle(
                                          color: Color(0xFF141414),
                                          fontSize: 14))
                                ],
                                text:widget.detailModel?.ornotOpen==0?'休息中 ':'营业中',
                                style: const TextStyle(
                                    color: Color(0xFFFA9A52), fontSize: 14)),
                          ),
                          const SizedBox(
                            height: 10,
                          ),
                          Text("电话：${widget.detailModel?.mobilePhone ?? ''}",
                              style: const TextStyle(
                                  color: Color(0xFF141414), fontSize: 14)),
                        ],
                      ),
                      GestureDetector(
                        onTap: () {
                          launchUrl(Uri(
                              scheme: "tel",
                              path: widget.detailModel?.mobilePhone ??
                                 CommonConfig.kfPhoneStr));
                        },
                        child: Column(
                          children: [
                            Image.asset(
                              'images/map/phone_shop_detail.png',
                              width: 30,
                            ),
                            const Text(
                              '电话',
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xFF141414)),
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                  const SizedBox(
                    height: 12,
                  ),
                  Container(
                    height: 1,
                    color: ThemeColor.dividerColor,
                  ),
                  const SizedBox(
                    height: 4,
                  ),
                  GestureDetector(
                    onTap: () {
                      if (CommonConfig.isShowPay() == false) {
                        return;
                      }
                      goToShop();
                    },
                    child: Row(
                      children: [
                        Expanded(
                          child: Text(
                            widget.detailModel?.address ?? '',
                            style: const TextStyle(
                                fontSize: 14,
                                color: Color(0xFF141414),
                                overflow: TextOverflow.ellipsis),
                          ),
                        ),
                        CommonConfig.isShowPay()
                            ? const Padding(
                                padding: EdgeInsets.only(top: 2),
                                child: Icon(
                                  Icons.arrow_forward_ios,
                                  size: 14,
                                  color: Color(0xFF141414),
                                ))
                            : Container(),
                      ],
                    ),
                  ),
                  const SizedBox(
                    height: 7,
                  ),
                  Row(
                    children: [
                      Image.asset(
                        'images/map/shop_details_location.png',
                        width: 14,
                      ),
                      Text(
                        '距您${widget.disStr}公里',
                        style: const TextStyle(
                            color: Color(
                              0xFF777777,
                            ),
                            fontSize: 12),
                      ),
                    ],
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                  Image.asset(
                    'images/map/shop_detail_bg.png',
                    height: 81,
                  ),
                  const SizedBox(
                    height: 21,
                  ),
                  Text(
                    '简介',
                    style: TextStyle(
                        fontSize: 18, color: ThemeColor.black1414Color),
                  ),
                  const SizedBox(
                    height: 6,
                  ),
                  Padding(
                    padding: const EdgeInsets.only(left: 10),
                    child: Text(
                      widget.detailModel?.introduce ?? '',
                      style: const TextStyle(
                          fontSize: 14, color: Color(0xFF777777)),
                    ),
                  ),
                  const SizedBox(
                    height: 21,
                  ),
                  Text(
                    '详情',
                    style: TextStyle(
                        fontSize: 18, color: ThemeColor.black1414Color),
                  ),
                  const SizedBox(
                    height: 6,
                  ),
                  (widget.detailModel?.matchProducts != null &&
                          widget.detailModel?.matchProducts != '')
                      ? Image.network(widget.detailModel?.matchProducts ?? '')
                      : Container(),
                ],
              ),
            ),
          ),
        ),
        Positioned(
          top: ScreenUtil().statusBarHeight + 14,
          left: 0,
          child: GestureDetector(
            onTap: () {
              Get.back();
            },
            child: Container(
              padding: const EdgeInsets.only(left: 10),
              child: Image.asset(
                'images/map/back_arrow_shop_detail.png',
                width: 30,
              ),
            ),
          ),
        ),
      ],
    );
  }

  void goToShop() {
    final size = MediaQuery.of(context).size;
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SizedBox(
            height: 240.0,
            child: Container(
                alignment: Alignment.center,
                child: Column(
                  children: [
                    GestureDetector(
                        onTap: () {
                          MapNavUtils.gotoAMapByKeywords(
                              widget.detailModel?.address,
                              widget.detailModel?.latitude,
                              widget.detailModel?.longitude);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          width: size.width,
                          height: itemHeight,
                          color: Colors.white,
                          child: const Text('高德地图'),
                        )),
                    Divider(
                      color: ThemeColor.sendMsgColor,
                      thickness: 7,
                      height: 1,
                    ),
                    GestureDetector(
                        onTap: () {
                          MapNavUtils.gotoBaiduByKeywords(
                              widget.detailModel?.address,
                              widget.detailModel?.latitude,
                              widget.detailModel?.longitude);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          width: size.width,
                          height: itemHeight,
                          color: Colors.white,
                          child: const Text('百度地图'),
                        )),
                    Divider(
                      color: ThemeColor.sendMsgColor,
                      thickness: 7,
                      height: 1,
                    ),
                    GestureDetector(
                        onTap: () {
                          MapNavUtils.gotoTencentMap(
                              widget.detailModel?.latitude,
                              widget.detailModel?.longitude);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          width: size.width,
                          height: itemHeight - 5,
                          color: Colors.white,
                          child: const Text('腾讯地图'),
                        )),
                    Divider(
                      color: ThemeColor.sendMsgColor,
                      height: 1,
                    ),
                    GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: Container(
                          alignment: Alignment.center,
                          width: size.width,
                          height: 55,
                          child: const Text('取消'),
                        ))
                  ],
                )));
      },
    );
  }

  @override
  void initState() {
    super.initState();
    getShopDetails();
  }

  void getShopDetails() async {
    var res = await HttpRequest.request('match-maker/business/queryDining',
        params: {"matchId": widget.detailModel?.id});
    if ((res is Map) && res['data'] != null) {
      setState(() {
        widget.detailModel=ShopMatchMakerListDataModel.fromJson(res['data'] );
      });
    }
  }
}
